<?php
namespace Home\Controller;

use Think\Cache\Driver\Memcache;
use Think\Controller;
use Common\Common\Common;
use Home\Model\UserModel;
use Common\Common\Cookie;
use Think\Cache\Driver\Redis;



class CommonController extends Controller{

    public $commonClass;
    public $userModel;
    public $Cookie;
    const MemcacheKey = 'mmy';
    const HomeUserInfo = 'HomeUserInfo';
    /**
     * 模板变量
     * @var array
     */
    public $data = array();

    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        $this->commonClass = new Common();
        $this->userModel = new UserModel();
        $this->Cookie = new Cookie();
        //初始化用户信息
        $this->initUserInfo();
    }

    /**
     * 生成验证码方法
     * @author mmy
     */
    public function verify()
    {
        $this->commonClass->verify();
    }

    /**
     * 检查验证码方法
     * @author mmy
     * @param $code
     * @param string $id
     */
    public function check_verify($code, $id)
    {
        return $this->commonClass->check_verify($code, $id);
    }
    /**
     * 检查用户是否登录
     * @author mmy
     */
    public function checkLogin()
    {
        $userInfo = session('HomeUserInfo');
        //游客不算登录
        if(($userInfo['levelid']>0))
        {
            return true;
        }
        return false;
    }

    /**
     * 设置用户session
     * @author mmy
     */
    public function setSession($arr)
    {
        if(count($arr)>0)
        {
            $userKey = $this->setMemcache($arr);
            $arr['userKey'] = $userKey;
            $arr['room_id'] = 1;
            session(self::HomeUserInfo,$arr);
        }
    }

    /**
     * 设置用户cookie
     * @author mmy
     */
    public function setCookie($arr)
    {
        if(count($arr)>0)
        {
            $userKey = $this->setMemcache($arr);
            $arr['userKey'] = $userKey;
            $arr['room_id'] = 1;
            $this->Cookie->set(self::HomeUserInfo, $this->Cookie->authcode(serialize($arr), 'ENCODE'), 86400*30);
        }
    }

    /**
     * 初始化用户信息
     * @author mmy
     */
    public function initUserInfo()
    {
        $session = $this->checkLogin();
        //检测用户session
        if(!$session)
        {
            //检测用户cookie
            $HomeUserInfo =unserialize($this->Cookie->authcode( $this->Cookie->get(self::HomeUserInfo),'DECODE'));
            if(!empty($HomeUserInfo))
            {
                //有cookie
                $this->setSession($HomeUserInfo);
            }
            else
            {
                //设置虚拟人
                $this->virtualUser();
            }
        }

    }

    /**
     * 生成虚拟人物
     * @author mmy
     */
    public function virtualUser()
    {
        $virNum = rand(111111,999999);
        $user = array(
             'userid'=>$virNum,
             'username'=>'游客'.$virNum,
             'nickname'=>'游客'.$virNum,
             'levelid'=>'-1',
             'ctime'=>date('Y-m-d H:i:s',time()),
             'loginlogid'=>'1',
             'realIp'=>$this->getUserIp(),
             'image'=>'/Public/home/images/default_head.jpg'

        );
        $this->setSession($user);
        //cookie有效时间一个月
        $this->Cookie->set(self::HomeUserInfo, $this->Cookie->authcode(serialize($user), 'ENCODE'), 86400*30);
    }

    /**
     * 获取用户真实ip
     * @author mmy
     */
    public function getUserIp()
    {
        if (getenv("HTTP_CLIENT_IP"))
            $clientIp = getenv("HTTP_CLIENT_IP");
        else if(getenv("HTTP_X_FORWARDED_FOR"))
            $clientIp = getenv("HTTP_X_FORWARDED_FOR");
        else if(getenv("REMOTE_ADDR"))
            $clientIp = getenv("REMOTE_ADDR");
        else
            $clientIp = "Unknow";

        return $clientIp;
    }

    /**
     * 设置memcache 用户信息
     * @author mmy
     */
    public function setMemcache($user)
    {
        $arr = array(
            'host'=>'127.0.0.1',
            'port'=>'11211',
        );
        $Memcache = new Memcache($arr);
        $userKey = md5(self::MemcacheKey.$user['id']);
        $Memcache->set($userKey,$user);

        return $userKey;
    }

    /**
     * 测试redis
     */
    public function redis()
    {
        $redis = new \Redis();
        $connect = $redis->connect('127.0.0.1',6379);
        $set = $redis->set('test','hello world!');
        $get = $redis->get("test");

        var_dump($redis);
        var_dump($connect);
        var_dump($set);
        var_dump($get);

    }

    public function memcache()
    {
        $arr = array(
            'host'=>'127.0.0.1',
            'port'=>'11211',
        );
        $Memcache = new Memcache($arr);
        $is = $Memcache->set('121','maomao');
        $get = $Memcache->get('121');
        var_dump($get);
    }


}